package com.example.xposed_apk;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;

import com.example.xposed_apk.Utils.AntiScreenSecure;
import com.example.xposed_apk.Utils.BypassUtils;
import com.example.xposed_apk.Utils.NetWork;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MainHook implements IXposedHookLoadPackage {

    @Override
    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        // Hook Application.attach(Context) 方法
        XposedHelpers.findAndHookMethod(
                Application.class,
                "attach",
                Context.class,
                new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        // 获取应用上下文
                        Context context = (Context) param.args[0];
                        if (context == null) {
                            XposedBridge.log("[MainHook] 获取 Context 失败，跳过处理");
                            return;
                        }

//                        String packageName = context.getPackageName();
//                        XposedBridge.log("[MainHook] 当前运行 APK 包名：" + packageName);
//
//                        // 获取并打印权限列表
//                        List<String> permissions = getAppPermissions(context, packageName);
//                        logPermissions(packageName, permissions);
//
//                        //hook网络请求库进行抓包
//                        XposedBridge.log("[MainHook] Apk抓包结果如下：" );
//                        NetWork.hookAllNetworkLibs(lpparam);
//                        BypassUtils.hookVPNCheck(lpparam);   // VPN相关检测

                        AntiScreenSecure.bypassScreenSecure(lpparam); //绕过禁止录屏
                    }
                }
        );
        if (!lpparam.packageName.equals("target.app.package")) return;

    }

    // 获取应用声明的权限列表
    private List<String> getAppPermissions(Context context, String packageName) {
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
            if (packageInfo.requestedPermissions != null) {
                return Arrays.asList(packageInfo.requestedPermissions);
            } else {
                return Collections.emptyList();
            }
        } catch (PackageManager.NameNotFoundException e) {
            XposedBridge.log("[MainHook] 获取权限时发生异常: " + e.getMessage());
            return Collections.emptyList();
        }
    }

    // 格式化并打印权限信息
    private void logPermissions(String packageName, List<String> permissions) {
        XposedBridge.log("\n========= 权限报告 [" + packageName + "] =========");
        if (permissions.isEmpty()) {
            XposedBridge.log("无任何权限声明");
        } else {
            XposedBridge.log("共 " + permissions.size() + " 项权限:");
            for (int i = 0; i < permissions.size(); i++) {
                String prefix = (i == permissions.size() - 1) ? "└─ " : "├─ ";
                XposedBridge.log(prefix + permissions.get(i));
            }
        }
        XposedBridge.log("==================================\n");
    }
}
